package config

import (
	"github.com/spf13/viper"
	"time"
)

// https://blog.csdn.net/qq_43716830/article/details/132926569
type DefaultConfigOper struct {
	configInst *viper.Viper
}

func NewDefaultConfigOper() *DefaultConfigOper {
	conf := &DefaultConfigOper{}
	conf.configInst = viper.New()
	conf.configInst.SetConfigType("yaml")
	return conf
}

// Get 返回一个 interface{} 类型的值，允许更灵活的类型转换
func (n *DefaultConfigOper) Get(keyName string) interface{} {
	// 实现逻辑，根据 keyName 获取配置值并转换为 interface{}
	return n.configInst.Get(keyName)
}

// GetString 根据 keyName 获取字符串类型的配置值
func (n *DefaultConfigOper) GetString(keyName string) string {
	return n.configInst.GetString(keyName)
}

// 其他 Get 方法的实现逻辑类似，根据需要转换类型
func (n *DefaultConfigOper) GetBool(keyName string) bool {
	return n.configInst.GetBool(keyName)
}

func (n *DefaultConfigOper) GetInt(keyName string) int {
	// 类型断言并转换为 int
	return n.configInst.GetInt(keyName)
}

func (n *DefaultConfigOper) GetInt32(keyName string) int32 {
	return n.configInst.GetInt32(keyName)
}

func (n *DefaultConfigOper) GetInt64(keyName string) int64 {
	// 类型断言并转换为 int64
	return n.configInst.GetInt64(keyName)
}

func (n *DefaultConfigOper) GetFloat64(keyName string) float64 {
	return n.configInst.GetFloat64(keyName)
}

func (n *DefaultConfigOper) GetDuration(keyName string) time.Duration {
	return n.configInst.GetDuration(keyName)
}

func (n *DefaultConfigOper) GetStringSlice(keyName string) []string {
	// 类型断言并转换为 []string
	return n.configInst.GetStringSlice(keyName)
}

func (n *DefaultConfigOper) GetStringMapString(key string) map[string]string {
	return n.configInst.GetStringMapString(key)
}
func (n *DefaultConfigOper) GetStringMapStringSlice(key string) map[string][]string {
	return n.configInst.GetStringMapStringSlice(key)
}
func (n *DefaultConfigOper) GetStringMap(key string) map[string]any {
	return n.configInst.GetStringMap(key)
}

func (n *DefaultConfigOper) SetDefaultString(keyName string, val string) {
	// 设置默认值
	n.configInst.SetDefault(keyName, val)
}

func (n *DefaultConfigOper) Unmarshal(rawVal interface{}) error {
	err := n.configInst.Unmarshal(rawVal)
	return err
}
